package com.cakeclient;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.Parameters;
import com.cakeclient.facebookmodel.LoginButton;
import com.cakeclient.facebookmodel.SessionEvents;

/**
 * Created by IntelliJ IDEA.
 * User: isen
 * Date: 29/03/12
 * Time: 22:07
 * To change this template use File | Settings | File Templates.
 */
public class ClientParameterFragment extends SherlockFragment {

    private LoginButton mLoginButton;
    private TextView mText;
    private String[] permissions = {"user_birthday,publish_stream"} ;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.client_parameter_layout, container, false);

    }





    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        RadioButton buttonYes = (RadioButton) getSherlockActivity().findViewById(R.id.radio_yes);
        RadioButton buttonNo = (RadioButton) getSherlockActivity().findViewById(R.id.radio_no);

        if (Parameters.getAskForStore(getSherlockActivity().getApplicationContext())) {
            buttonYes.setChecked(true);
        } else {
            buttonNo.setChecked(true);
        }


        buttonYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    Parameters.setAskForStore(getSherlockActivity().getApplicationContext(), true);
                } else
                    Parameters.setAskForStore(getSherlockActivity().getApplicationContext(), false);

            }
        });




        //Réglages Facebook:

        mText = (TextView) ClientParameterFragment.this.getSherlockActivity().findViewById(R.id.client_parameter_facebook_textview);
        mLoginButton = (LoginButton) ClientParameterFragment.this.getSherlockActivity().findViewById(R.id.login);



        SessionEvents.addAuthListener(new SampleAuthListener());
        SessionEvents.addLogoutListener(new SampleLogoutListener());



        mLoginButton.init(getSherlockActivity(),((MainParametersActivity)getActivity()).mFacebook,permissions);

    }

    //Listenners Facebook
    public class SampleAuthListener implements SessionEvents.AuthListener {

        public void onAuthSucceed() {
            mText.setText("You have logged in! ");

            // Ici on va sauvegarder les infos utiles dans les shared préference !
            ((MainParametersActivity)getSherlockActivity()).updateInformations();
        }

        public void onAuthFail(String error) {
            mText.setText("Login Failed: " + error);
        }
    }

    public class SampleLogoutListener implements SessionEvents.LogoutListener {
            public void onLogoutBegin() {
                mText.setText("Logging out...");
            }

            public void onLogoutFinish() {
                mText.setText("You have logged out! ");
            }
        }

}
